import {observable, action} from "mobx";
import {Form} from "weaver-mobile-page";
import {Toast, Tools} from "weaver-mobile";
import * as API from "../../api/cptApi";

const getLabel = Tools.getLabel;

export default class OperationsStore {
    @observable isLoading = true;
    @observable form = new Form.Store();
    @observable nowPath;
    @observable hasAdMsg = false;
    @observable cptid = "";//资产id
    @observable cptname = "";//资产name

    @action
    init = (pathName) => {
        this.nowPath = pathName;
        this.getFetchForm(pathName)
    };

    @action
    onAdMsg = (hasAdMsg, id = "", name = "") => {//判断是否传来cptid和cptname，具有则为送修和归还
        this.hasAdMsg = hasAdMsg;
        this.cptid = id;
        this.cptname = decodeURI(name);

    };

    @action
    formChange = (field, value) => {
        // console.log("form事件触发：", field, toJS(value))
        if (field.conditionType === "BROWSER") {
            if (field.browserType === "23") {   //browserType 23:资产
                if (field.linkUrl) {
                    let params = {};
                    if (value.length > 0) {
                        params = {cptid: value[0].id};

                        API.getData({
                            url: field.linkUrl,
                            params: params
                        }).then(res => {
                            field.linkKey.forEach((item) => {
                                let key = item, val = res[item];
                                if (this.nowPath === "cptchange") {//处于且仅处于资产变动功能时，
                                    // 当被联动字段是浏览框时，需要将之拼接为[{id:'',name:''}]的形式
                                    if (item === "capitalgroupid" || item === "resourceid" || item === "blongdepartmentid" || item === "warehouse") {
                                        val = [{
                                            id: res[item],
                                            name: res[item + "span"]
                                        }]
                                    }
                                }else if (this.nowPath === "cptmove") {//处于且仅处于资产调拨功能时，
                                    // 当被联动字段是浏览框时，需要将之拼接为[{id:'',name:''}]的形式
                                    if (item === "warehouse") {
                                        val = [{
                                            id: res[item],
                                            name: res[item + "span"]
                                        }]
                                    }
                                }
                                this.updateField(key, val);
                            })
                        })
                    }
                }
            }

        }
    };
    @action
    getFetchForm = (pathName) => {
        this.isLoading = true;
        const params = {
            mtype: pathName
        };
        API.getCptManagerForm(params).then((res) => {
            this.form = new Form.Store()
            this.form.initForm(res.fieldinfo, "/operations/" + pathName);
            if (!this.hasAdMsg) {
                this.isLoading = false;
            } else {
                this.updateField("capitalid", [{id: this.cptid, name: this.cptname}]);
                res.fieldinfo[0].items.forEach((el) => {
                    if (el.conditionType === "BROWSER") {
                        if (el.browserType === "23") {   //browserType 23:资产
                            this.isLoading = false;
                            this.formChange(el, [{id: this.cptid, name: this.cptname}]);
                        }
                    }
                });
            }
        });
    };
    @action
    cancelThat = (history) => {
        history.go(-1);
        this.clearForm();
    };
    @action
    checkTheInfo = (history, method) => {
        const paramsAll = this.getParams();
        const {capitalid, hrmid} = paramsAll;
        let number = 1;
        // if (!capitalid) {
        //     Toast.fail(getLabel(389846, "请选择资产"), 2);
        //     return
        // }
        // if (this.nowPath === "cptuse" || this.nowPath === "cptmove"|| this.nowPath === "cptlend") {
        //     if (!hrmid) {
        //         Toast.fail(getLabel(390197, "请选择申请人"), 2);
        //         return
        //     }
        // }
        let re = this.form.doValidate();
        if(!re.pass){
            return;
        }
        
        if (this.nowPath === "cptloss" || this.nowPath === "cptdiscard") {
            number = paramsAll["capitalcount"];
        } else if (this.nowPath === "cptuse"||this.nowPath === "cptchange") {
            number = paramsAll["capitalnum"];
        } else if (this.nowPath == "cptmove") {
            number = paramsAll["availablenum"];
        }
        

        // if (this.nowPath === "cptmove") {
        //     if (!paramsAll["cptdept_to"]) {
        //         Toast.fail(getLabel(389848, "请选择调往的部门"), 2);
        //         return
        //     }
        // }
        

        let params = {
            poststr: capitalid + "," + number
        };
        if(this.nowPath === "cptchange"){
            params = {poststr: capitalid + "," + number,cpttype:"change"}
        }
        this.isLoading = true;
        API.doCptIfOverAjax(params).then(res => {
            this.isLoading = false;
            if (res.msg === "") {
                this.submitInfo(history, method);
            } else {
                Toast.fail(res.msg, 3);
            }
        })
    };


    @action
    submitInfo = (history, method) => {
        const paramsAll = this.getParams();
        let params = {
            ...paramsAll,
            mtype: this.nowPath
        };
        if (method) {
            params = {
                ...params,
                method: "backMyCpt"
            }
        }
        this.isLoading = true;
        API.doCptManager(params).then(res => {
            this.isLoading = false;
            if (res.msg === "") {
                this.init(this.nowPath);
                Toast.success(getLabel(30700, "操作成功"), 2);
                if (this.hasAdMsg) {
                    history.go(-1);
                    history.replace("/assetInfo/infoCard/" + this.cptid);
                }
            } else {
                Toast.fail(res.msg, 2);
            }
        })
    };
    @action
    scanQRCodeCallback = (data) => {
        this.isLoading = true;
        if (data) {
        const params = {
            scantype: this.nowPath,
            mark: data
        };
        API.scanQRCodeCapital(params).then(res => {
            this.isLoading = false;
            if (res.flag) {
                this.form = new Form.Store();
                this.form.initForm(res.fieldinfo, "/operations/" + this.nowPath);

            } else {
                Toast.fail(res.msg, 2);
            }
        })
        }
    }

    @action
    getParams = () => {
        return this.form.formParams;
    };

    @action
    updateField = (key, value) => {
        this.form.updateField(key, value);
    };
    @action
    updateFields = (fields) => {
        this.form.updateFields(fields)
    };
    @action
    clearForm = () => {
        this.form.resetFields();
    };
}
